﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UFIDA.U9.Base;
using UFIDA.U9.CBO.PubSV.Helper;
using UFIDA.U9.CBO.SCM.Customer;
using UFIDA.U9.CBO.SCM.Item;
using UFIDA.U9.ISV.Customer;
using UFSoft.UBF.Business;
using UFSoft.UBF.PL;
using UFSoft.UBF.Util.Context;
using CustomerDTO = UFIDA.U9.ISV.Customer.CustomerDTO;
using CustomerDTOData = UFIDA.U9.ISV.Customer.CustomerDTOData;
using CustomerSiteDTO = UFIDA.U9.ISV.Customer.CustomerSiteDTO;

namespace ufida.u9.ait.openapi.utils
{
    internal class CustomerUtils
    {
		//该代码参考 BatchModifyCustomerByDTOSV 修改
		public static List<CustomerData> AITBatchModifyCustomerByDTOSV(List<CustomerDTOData> CustomerDTOs)
        {
            //Customer.EntityList entityList = new Customer.EntityList();
			List<CustomerData> retlist = new List<CustomerData>();
			long orgID = -1L;
			foreach (CustomerDTOData customerDTOData in CustomerDTOs)
			{
				//用上下文赋值org
				customerDTOData.Org = new UFIDA.U9.CBO.Pub.Controller.CommonArchiveDataDTOData()
				{
					ID = Context.LoginOrg.ID,
					Code = Context.LoginOrg.Code,
				};

				CustomerDTO customerDTO = new CustomerDTO();
				customerDTO.FromEntityData(customerDTOData);
				try
				{
					using (ISession session = Session.Open())
					{
						Customer customer = null;
						if (customerDTO.ID > 0L)
						{
							customer = Customer.Finder.FindByID(customerDTO.ID);
						}
						else if (!string.IsNullOrEmpty(customerDTO.Code))
						{
							if (customerDTO.Org != null)
							{
								if (customerDTO.Org.ID > 0L)
								{
									customer = Customer.Finder.Find("Code=@Code and Org=@Org", new OqlParam[]
									{
										new OqlParam(customerDTO.Code),
										new OqlParam(customerDTO.Org.ID)
									});
								}
								else if (!string.IsNullOrEmpty(customerDTO.Org.Code))
								{
									customer = Customer.Finder.Find("Code=@Code and Org.Code=@OrgCode", new OqlParam[]
									{
										new OqlParam(customerDTO.Code),
										new OqlParam(customerDTO.Org.Code)
									});
								}
								else
								{
									customer = Customer.Finder.Find("Code=@Code and Org.Code=@OrgCode", new OqlParam[]
									{
										new OqlParam(customerDTO.Code),
										new OqlParam(PlatformContext.Current.OrgCode)
									});
								}
							}
						}
						if (customer == null)
							throw new Exception("客户修改方法,查找客户失败!");

						if (customer != null)
						{
							orgID = customer.OrgKey.ID;
							CBOCommonHelper.DtoToEntity<CustomerDTO, Customer>(customerDTO, customer, delegate (DTOToEntityEventArgs<CustomerDTO, Customer> e)
							{
								e.OrgID = orgID;
								if (e.AttributeName == "CustomerSiteDTOs" || e.AttributeName == "DTOForCRM")
								{
									e.Cancel = true;
								}
							});

							if (customerDTO.CustomerSiteDTOs != null && customerDTO.CustomerSiteDTOs.Count > 0)
							{
								foreach (CustomerSiteDTO customerSiteDTO in customerDTO.CustomerSiteDTOs)
								{
									CustomerSite customerSite = CustomerSite.Finder.Find($"Code='{customerSiteDTO.Code}' and Customer.Code='{customerDTO.Code}' and Customer.Org.Code='{customerDTO.Org.Code}'");
									if(customerSite == null)
                                    {
										customerSite = CustomerSite.Create(customer);
									}
									
									CBOCommonHelper.DtoToEntity<CustomerSiteDTO, CustomerSite>(customerSiteDTO, customerSite, delegate (DTOToEntityEventArgs<CustomerSiteDTO, CustomerSite> e)
									{
										e.OrgID = orgID;
										if (e.AttributeName == "OperateType")
										{
											e.Cancel = true;
										}
									});
								}
							}
							session.Commit();
							//entityList.Add(customer);
							retlist.Add(customer.ToEntityData());
						}
					}
				}
				catch (Exception ex)
				{
					throw new CommonCodeSVException(customerDTO.Code, Customer.EntityRes.GetResource(Customer.EntityRes.BE_Name), ex.Message);
				}
			}
			return retlist;
		}
    }
}
